package team18.yuejian.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import team18.yuejian.Model.CommonModel;
import team18.yuejian.R;
import team18.yuejian.RetrofitListener;
import team18.yuejian.bean.LoginBean;

public class LoginActivity extends BaseActivity implements View.OnClickListener,RetrofitListener<LoginBean> {

    private EditText unameView,upasswordView;

    private TextView reg;
    private TextView repassword;
    private String username;
    private String password;
    private Button login;


    SharedPreferences sp;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initEvent();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1 :
                if (resultCode == RESULT_OK){
                    username = data.getStringExtra("username");
                    password = data.getStringExtra("password");
                    unameView.setText(username);
                    upasswordView.setText(password);
                }
                break;
            default:
        }
    }


    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()){
            case R.id.login_reg:
                intent = new Intent(LoginActivity.this,RegActivity.class);
                Toast.makeText(LoginActivity.this,"跳转到注册界面",Toast.LENGTH_SHORT).show();
                startActivityForResult(intent,1);
                break;

            case R.id.login_repassword:
                intent = new Intent(LoginActivity.this,RePasswordActivity.class);
                Toast.makeText(LoginActivity.this,"跳转到忘记密码界面",Toast.LENGTH_SHORT).show();
                startActivity(intent);
                break;

            case R.id.login_bt:
                CommonModel commonModel = new CommonModel();
                commonModel.UserLogin(unameView.getText().toString(),upasswordView.getText().toString(),this);

                break;
        }
    }

    @Override
    public void onSuccess(LoginBean loginBean, int flag) {
        if ("0".equals(loginBean.getError())){
            Toast.makeText(LoginActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
        }else if ("1".equals(loginBean.getError())){
            Toast.makeText(LoginActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
        }else {
            setUserId(loginBean.getUid());
            Intent intent = new Intent(LoginActivity.this,MineActivity.class);
            Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
            startActivity(intent);
        }
    }

    @Override
    public void onFail(String msg) {

    }


    public void initView(){
        unameView=findViewById(R.id.login_username);
        upasswordView=findViewById(R.id.login_password);
        reg = findViewById(R.id.login_reg);
        repassword = findViewById(R.id.login_repassword);
        login = findViewById(R.id.login_bt);
    }

    public void initEvent(){
        reg.setOnClickListener(this);
        repassword.setOnClickListener(this);
        login.setOnClickListener(this);
    }



}
